class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int left = 0, right = 0, len = 0;
        unordered_map<int, int> count;

        while (right < fruits.size())
        {
            count[fruits[right]]++;

            while (count.size() > 2)
            {
                count[fruits[left]]--;

                if (count[fruits[left]] == 0)
                    count.erase(fruits[left]);

                left++;
            }
            len = max(len, right - left + 1);
            right++;
        }

        return len;
    }
};